package com.aube.plugin.episode;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.utils.SysUtil;
import com.huyn.baseframework.utils.Utils;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by huyaonan on 16/5/17.
 */
public class VideoItem extends DLLayout {

    private Context context;
    private IPositionManager mManager;

    private AtomicBoolean mAnimated = new AtomicBoolean(false);

    private ImageView mImg;
    private TextView mText;
    private TextView mDes;
    private View mImgGroup;
    private int mItemWidth;

    public VideoItem(Context context) {
        this(context, null);
    }
    public VideoItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public VideoItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @Override
    public void initViews() {
        inflate(R.layout.item_video, this, true);

        mImg = (ImageView) findViewById(R.id.video_img);
        mText = (TextView) findViewById(R.id.video_title);
        mDes = (TextView) findViewById(R.id.video_des);
        mImgGroup = findViewById(R.id.video_img_group);

        mText.setAlpha(0f);
    }

    @Override
    public void fillData(Object obj) {
        if(obj == null || !(obj instanceof VideoModel))
            return;
        int width = SysUtil.getAbsoluteScreenWidth(context);
        int space = getResources().getDimensionPixelOffset(R.dimen.widget_margin);
        int itemWidth = (width-4*space)/2;
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mImgGroup.getLayoutParams();
        params.width = itemWidth;
        params.height = itemWidth*9/16;
        mImgGroup.setLayoutParams(params);

        ImageLoader.getInstance(context).fillImage(mImg, ((VideoModel) obj).pic);
    }

    public void setupContent(Object obj, int width) {
        setupContent(obj, width, false);
    }

    public void setupContent(Object obj, int width, boolean lowHeight) {
        if(obj == null || !(obj instanceof VideoModel))
            return;
        mItemWidth = width;
        VideoModel item = (VideoModel) obj;
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mImgGroup.getLayoutParams();
        params.width = width;
        params.height = lowHeight ? width*10/23 : width*9/16;
        mImgGroup.setLayoutParams(params);

        mText.setText(item.videoTitle);
        mDes.setText(item.videoDesc);
        mImg.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bk_default));

        int mRequestW = 400;
        int mRequestH = 400;
        if(lowHeight) {
            mRequestW = mRequestH * 23/10;
        } else {
            mRequestH = mRequestW*9/16;
        }
        ImageLoader.getInstance(context).fillImage(mImg, item.pic, mRequestW, mRequestH);
    }

    @Override
    public void notifyAnim(int animDelay) {
//        if(mManager == null)
//            return;
//        if(mAnimated.get())
//            return;
//        mAnimated.set(true);
//        setVisibility(View.VISIBLE);

        ObjectAnimator imgAlpha = ObjectAnimator.ofFloat(this, "alpha", 0, 1f);
        imgAlpha.setDuration(300);
        imgAlpha.setStartDelay(animDelay);

        ObjectAnimator imgTranslate = ObjectAnimator.ofFloat(this, "translationX", mItemWidth/2, 0);
        imgTranslate.setDuration(300);
        imgAlpha.setStartDelay(animDelay);

        ObjectAnimator txtAlpha = ObjectAnimator.ofFloat(mText, "alpha", 0, 1f);
        txtAlpha.setDuration(300);
        txtAlpha.setStartDelay(200+animDelay);

        AnimatorSet mSet = new AnimatorSet();
        mSet.playTogether(imgAlpha, imgTranslate, txtAlpha);
        mSet.start();
    }

    @Override
    public void registToManager(IPositionManager manager) {
        if(mManager != null || manager == null)
            return;
        manager.add(this);
        this.mManager = manager;
        setVisibility(View.INVISIBLE);
    }

    @Override
    public void unreistFromManager(IPositionManager manager) {
        if(mManager == null || manager == null)
            return;
        manager.remove(this);
        this.mManager = null;
    }

    @Override
    public int[] getLocations() {
        int[] locs = new int[2];
        getLocationOnScreen(locs);
        return locs;
    }

    public ImageView getImageView() {
        return mImg;
    }

}
